package com.liit.front.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.liit.front.filter.WebContext;
import com.liit.module.dto.PathDto;
import com.liit.module.entity.Dict;
import com.liit.module.entity.Files;
import com.liit.module.entity.Product;
import com.liit.module.frame.utils.Const;
import com.liit.module.frame.utils.FileUtil;
import com.liit.module.frame.utils.page.Pagination;
import com.liit.module.service.DictService;
import com.liit.module.service.FilesService;
import com.liit.module.service.ProductService;

/**
 * @author dingweibo
 * @Title: ProductController.java
 * @Description:产品相关
 * @date 2015年12月2日
 */
@Controller
@RequestMapping(value = "/product")
public class ProductController {

	@Resource
	ProductService productService;

	@Resource
	DictService dictService;

	@Resource
	FilesService filesService;

	/**
	 * 产品项
	 * 
	 * @return
	 */
	@RequestMapping(value = "/{id}")
	public String item(@PathVariable Integer id, @RequestParam(required = false) Integer type, Pagination pagination) {
		List<Dict> productType = dictService.selectByColumn(Const.PRODUCT_TYPE);
		WebContext.setAttribute("productType", productType);

		Product record = new Product();
		record.setCategory(id);
		if (type != null) {
			record.setType(type);
		}
		pagination.setRows(8);// 每页8条记录
		Map<String, Object> result = productService.listPage(pagination, record);
		List<Product> list = (List<Product>) result.get("list");
		List<PathDto> paths = new ArrayList<PathDto>();
		for (Product p : list) {
			PathDto dto = new PathDto();
			dto.setId(p.getId());
			dto.setName(p.getName());
			dto.setContent(p.getContent() == null ? "" : p.getContent().length() > 25 ? p.getContent().substring(0, 25) + "..." : p.getContent());
			dto.setPath(FileUtil.getUrl(filesService.selectById(p.getImgLogo())));
			dto.setType(p.getType());
			paths.add(dto);
		}

		BeanUtils.copyProperties(result.get("page"), pagination);
		WebContext.setAttribute("paths", paths);
		WebContext.setAttribute(Const.PAGINATION, pagination);
		IndexController.navigation();
		return "/product/productItem";
	}

	/**
	 * 产品项
	 * 
	 * @return
	 */
	@RequestMapping(value = "/detail/{id}")
	public String detail(@PathVariable Integer id) {
		if (id == null) {
			throw new NullPointerException("未找到产品信息");
		}
		Product product = productService.selectById(id);
		if (product == null) {
			throw new NullPointerException("未找到产品信息");
		}
		if (product.getImgOne() != null) {
			Files files = filesService.selectById(product.getImgOne());
			WebContext.setAttribute("imgOne", FileUtil.getUrl(files));
		}
		if (product.getImgTwo() != null) {
			Files files = filesService.selectById(product.getImgTwo());
			WebContext.setAttribute("imgTwo", FileUtil.getUrl(files));
		}
		if (product.getImgThree() != null) {
			Files files = filesService.selectById(product.getImgThree());
			WebContext.setAttribute("imgThree", FileUtil.getUrl(files));
		}
		if (product.getImgFour() != null) {
			Files files = filesService.selectById(product.getImgFour());
			WebContext.setAttribute("imgFour", FileUtil.getUrl(files));
		}
		if (product.getImgFive() != null) {
			Files files = filesService.selectById(product.getImgFive());
			WebContext.setAttribute("imgFive", FileUtil.getUrl(files));
		}
		if (product.getImgSix() != null) {
			Files files = filesService.selectById(product.getImgSix());
			WebContext.setAttribute("imgSix", FileUtil.getUrl(files));
		}
		if (product.getImgSeven() != null) {
			Files files = filesService.selectById(product.getImgSeven());
			WebContext.setAttribute("imgSeven", FileUtil.getUrl(files));
		}
		if (product.getImgEight() != null) {
			Files files = filesService.selectById(product.getImgEight());
			WebContext.setAttribute("imgEight", FileUtil.getUrl(files));
		}
		if (product.getImgNine() != null) {
			Files files = filesService.selectById(product.getImgNine());
			WebContext.setAttribute("imgNine", FileUtil.getUrl(files));
		}
		if (product.getImgTen() != null) {
			Files files = filesService.selectById(product.getImgTen());
			WebContext.setAttribute("imgTen", FileUtil.getUrl(files));
		}
		if (product.getImgEleven() != null) {
			Files files = filesService.selectById(product.getImgEleven());
			WebContext.setAttribute("imgEleven", FileUtil.getUrl(files));
		}
		if (product.getImgTwelve() != null) {
			Files files = filesService.selectById(product.getImgTwelve());
			WebContext.setAttribute("imgTwelve", FileUtil.getUrl(files));
		}
		if (product.getImgThirteen() != null) {
			Files files = filesService.selectById(product.getImgThirteen());
			WebContext.setAttribute("imgThirteen", FileUtil.getUrl(files));
		}
		if (product.getImgFourteen() != null) {
			Files files = filesService.selectById(product.getImgFourteen());
			WebContext.setAttribute("imgFourteen", FileUtil.getUrl(files));
		}
		if (product.getImgFifteen() != null) {
			Files files = filesService.selectById(product.getImgFifteen());
			WebContext.setAttribute("imgFifteen", FileUtil.getUrl(files));
		}
		if (product.getImgSixteen() != null) {
			Files files = filesService.selectById(product.getImgSixteen());
			WebContext.setAttribute("imgSixteen", FileUtil.getUrl(files));
		}
		if (product.getImgSeventeen() != null) {
			Files files = filesService.selectById(product.getImgSeventeen());
			WebContext.setAttribute("imgSeventeen", FileUtil.getUrl(files));
		}
		if (product.getImgEighteen() != null) {
			Files files = filesService.selectById(product.getImgEighteen());
			WebContext.setAttribute("imgEighteen", FileUtil.getUrl(files));
		}
		if (product.getImgNineteen() != null) {
			Files files = filesService.selectById(product.getImgNineteen());
			WebContext.setAttribute("imgNineteen", FileUtil.getUrl(files));
		}
		if (product.getImgTwenty() != null) {
			Files files = filesService.selectById(product.getImgTwenty());
			WebContext.setAttribute("imgTwenty", FileUtil.getUrl(files));
		}
		WebContext.setAttribute("product", product);

		IndexController.navigation();
		return "/product/productItemDetail";
	}
}
